//save name setting

/*
    同名设置对话框加载
*/
const Cc = Components.classes;
const Ci = Components.interfaces;

const leagueNames = getStringPreference("setting.leaguenames", false);
const teamNames = getStringPreference("setting.teamnames", false);

var macauLeagues = []; //联盟信息
var macauTransOrder = []; //让球信息

var macauLeagueLive = null;

var lijiLeagues = [];
var lijiTransOrder = [];

var shabaLeagues = [];
var shabaTransOrder = [];

var newballLeagues = [];
var newballTransOrder = [];

function $(id) {
  return document.getElementById(id);
}

function jsdump(str) {
  Cc['@mozilla.org/consoleservice;1'].getService(Ci.nsIConsoleService).logStringMessage(str);
}

//把父窗口的信息传递过来，方便调用
function initParams() {
  macauLeagues = window.opener.macauLeagues;
  macauTransOrder = window.opener.macauTransOrder;
  macauLeagueLive = window.opener.macauLeagueLive;
  lijiLeagues = window.opener.lijiLeagues;
  lijiTransOrder = window.opener.lijiTransOrder;
  shabaLeagues = window.opener.shabaLeagues;
  shabaTransOrder = window.opener.shabaTransOrder;
  newballLeagues = window.opener.newballLeagues;
  newballTransOrder = window.opener.newballTransOrder;

  return true;
}

function initLeagueLists() {
  //jsdump("macauLeagueLive: " + macauLeagueLive);
  for (var i = 0; i < macauLeagueLive; i++) {
    $("list.league.macau").appendItem(macauLeagues[i].name);
  }
  for (var i = 0; i < lijiLeagues.length; i++) {
    $("list.league.liji").appendItem(lijiLeagues[i].name);
  }
  for (var i = 0; i < shabaLeagues.length; i++) {
    $("list.league.shaba").appendItem(shabaLeagues[i].name);
  }
  for (var i = 0; i < newballLeagues.length; i++) {
    $("list.league.newball").appendItem(newballLeagues[i].name);
  }
}

function nameSettingLoad() {  
  window.sizeToContent();
  if (initParams()) {
    initLeagueLists();
  }
}

function createListItems(listID) {
  var list = $(listID);
  for (var i = 0; i < 10; i++) {
    var item = list.appendItem(listID + i, "");
  }
}

function saveNameSetting() {
  jsdump("saveNameSetting");
  window.opener.saveXMLByName(leagueNames, window.opener.domLeagues);
  jsdump("leagueNames: " + leagueNames);
  window.opener.saveXMLByName(teamNames, window.opener.domTeams);
  jsdump("teamNames: " + teamNames);
  return window.close();
}

//进行通用的处理，就是显示和清空textbox.value,list.selection
function listBoxClicked(event) {
  var list = event.currentTarget;
  var target = event.target;
  //如果选中了item，就显示当前选中的，否则清楚原来选择的    
  if (target.tagName == "listitem") {
    var selItem = list.selectedItem;
    var textBox = list.previousSibling;
    textBox.value = selItem.label;
  } else {
    var textBox = list.previousSibling;
    textBox.value = "";
    list.clearSelection();
  }
}

function clearTeamLists(listbox) {
  var count = listbox.itemCount;
  for (var i = count - 1; i >= 0; i--) {
    listbox.removeItemAt(i);
  }
}
/*
    添加新的联赛名称
*/
function addLeagueName() {
  var domLeagues = window.opener.domLeagues;
  var groupBox = document.getElementById("group.leagues");
  var textBoxes = groupBox.getElementsByTagName("textbox");
  var leagues = [];
  var str = "";
  for (var i = 0; i < textBoxes.length; i++) {
    if (textBoxes[i].value != "" && textBoxes[i].value != null) {
      leagues.push(textBoxes[i].value);
      str = str + leagues[i] + "=";
    }
  }
  if (str.lastIndexOf('=') == str.length - 1) str = str.substring(0, str.length - 1);
  jsdump(str);

  if (leagues.length >= 2) {
    var root = domLeagues.getElementsByTagName("Leagues")[0];
    var elems = root.getElementsByTagName("League");
    for (var i = 0; i < elems.length; i++) {
      var text = elems[i].textContent;
      if (text.search(leagues[0]) > -1) {
        jsdump("修改记录: " + leagues[0] + " text: " + text);
        elems[i].textContent = str;
        return;
      }
    }
    //添加新的记录
    jsdump("添加新记录");
    var newLeague = domLeagues.createElement("League");
    newLeague.appendChild(domLeagues.createTextNode(str));
    root.appendChild(newLeague);
  }

}

/*
    添加新的球队名称 
*/
function addTeamName() {
  var domTeams = window.opener.domTeams;
  var groupBox = document.getElementById("group.teams");
  var textBoxes = groupBox.getElementsByTagName("textbox");
  var teams = [];
  var str = "";
  for (var i = 0; i < textBoxes.length; i++) {
    if (textBoxes[i].value != "") {
      teams.push(textBoxes[i].value);
      //str = str + teams[i] + "=";
      str = str.concat(teams[i], "=");      
    }
  }
  if (str.lastIndexOf('=') == str.length - 1) str = str.substring(0, str.length - 1);
  jsdump(str);
  if (teams.length >= 2) {
    var root = domTeams.getElementsByTagName("Teams")[0];
    var elems = root.getElementsByTagName("Team");
    var re = new RegExp("");
    for (var i = 0; i < elems.length; i++) {
      var text = elems[i].textContent;
      if (text.search(teams[0]) > -1) {
        jsdump("修改记录: " + teams[0] + " text: " + text);
        //elems[i].textContent = str;
        //根据是否有完全一致来添加数据，如果没有添加那就加到后面
        for(var j=0; j<textBoxes.length; j++) {
          var find = false;
          for(var k=0; k<teams.length; k++) {
            if (textBoxes[i] != undefined && textBoxes[i].value==teams[k]) {
              find = true;
              break;
            }
          }
          if(!find) {
            elems[i].textContent = elems[i].textContent.concat('=', textBoxes[j]);
          }
        }
        jsdump("修改记录: "+elems[i].textContent);
        return;
      }
    }
    //添加新的记录
    jsdump("添加新记录: "+str);
    var newTeam = domTeams.createElement("Team");
    newTeam.appendChild(domTeams.createTextNode(str));
    root.appendChild(newTeam);
  }
}

/* 
    联盟点击以后，显示联盟下的球队
*/
function leagueClicked(event, teamListID, transOrder, length) {
  listBoxClicked(event);
  var list = event.currentTarget;
  var selItem = list.selectedItem;

  if (selItem) {
    var league = selItem.label;
    var teamList = $(teamListID);
    clearTeamLists(teamList);
    var current = false;
    var pre = false;
    if (length == undefined) {
      length = transOrder.length;
    }
    //jsdump("length:" + length);
    for (var i = 0; i < length; i++) {
      //jsdump("transOrder[i][0]: " + transOrder[i][0] + "; league: " + league);
      if (transOrder[i].leagueName == league) {
        //当前球队与上一支球队同名，就不添加，继续进行查找
        if (i > 0 && (transOrder[i].homeTeam == transOrder[i - 1].homeTeam)) {
          pre = current = true;
          continue;
        }
        teamList.appendItem(transOrder[i].homeTeam);
        teamList.appendItem(transOrder[i].awayTeam);
        current = true;
      } else {
        current = false;
      }
      //如果当前球队不是联盟，但是上一支球队是，联盟的，就停止查找
      if (current == false && pre == true) {
        break;
      }
      pre = current;
    }
  }
}

function leagueMacauClicked(event) {
  leagueClicked(event, "list.team.macau", macauTransOrder, window.opener.macauLive);
}

function leagueLijiClicked(event) {
  leagueClicked(event, "list.team.liji", lijiTransOrder);
}

function leagueShabaClicked(event) {
  leagueClicked(event, "list.team.shaba", shabaTransOrder);
}

function leagueNewballClicked(event) {
  leagueClicked(event, "list.team.newball", newballTransOrder);
}

function teamMacauClicked(event) {
  listBoxClicked(event);
}

function teamLijiClicked(event) {
  listBoxClicked(event);
}

function teamShabaClicked(event) {
  listBoxClicked(event);
}

function teamNewballClicked(event) {
  listBoxClicked(event);
}